<template>
  <div class="wechat">
    <!-- 卡片视图区域 -->
    <div class="wechat_card">
      <!-- 布局 -->
      <el-row :gutter="20" class="wechat_card_head" type="flex">
        <el-col :span="8">
          <!-- 搜索框及添加区域 -->
          <el-input
            size="medium"
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getPayInfoList"
          >
            <el-button
              size="medium"
              slot="append"
              icon="el-icon-search"
              @click="getPayInfoList"
            ></el-button> </el-input
        ></el-col>
       <el-button
          size="medium"
          class="btn_head_medium"
          type="primary"
          @click="addDialogVisible = true"
          icon="el-icon-circle-plus-outline"
          >新增</el-button
        >
        <el-button
          size="medium"
          type="danger"
          @click="removeUserByIds"
          icon="el-icon-remove-outline"
          >删除</el-button
        >
        <!-- 表格列筛选 -->
        <div class="tools_head">
          <el-popover placement="bottom" trigger="click">
            <el-checkbox-group v-model="colOptions">
              <el-checkbox
                v-for="item in colSelect"
                :label="item"
                :key="item"
                style="display: block; margin: 10px"
              >
              </el-checkbox>
            </el-checkbox-group>
            <div slot="reference">
              <el-tooltip content="筛选列" placement="bottom">
                <el-button size="small" icon="el-icon-menu"></el-button>
              </el-tooltip>
            </div>
            <el-button
              size="small"
              type="text"
              @click="filterFunHandle('allchecked')"
              >全选</el-button
            >
            <el-button
              size="small"
              type="text"
              @click="filterFunHandle('cancel')"
              >取消全选</el-button
            >
          </el-popover>
          <!-- 导出EXCEL -->
          <el-tooltip content="导出excel" placement="bottom">
            <el-button
              size="small"
              icon="el-icon-download"
              style="margin-left: 10px"
              @click="handleDownload"
            ></el-button>
          </el-tooltip>
        </div>
      </el-row>
      <!-- 支出记录列表区域 -->
      <el-table :data="payInfoList" style="width: 100%" border stripe :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }" size="small" height="85%" 
        ref="tableDataRef">
        <el-table-column type="selection"></el-table-column>
        <el-table-column type="index" label="序号"  v-if="colData[0].istrue"></el-table-column>
        <el-table-column prop="projectId" label="科目"  v-if="colData[1].istrue"></el-table-column>
        <el-table-column prop="money" label="金额"  v-if="colData[2].istrue"></el-table-column>
        <el-table-column prop="payTime" label="入账时间"  v-if="colData[3].istrue">
           <template slot-scope="scope">
            {{ scope.row.addTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="会计审核状态" v-if="colData[4].istrue" :filters="[{ text: '待审核', value: 0 }, { text: '通过', value: 1 }, { text: '驳回', value: 2 }]"
      :filter-method="filterTag"
      filter-placement="bottom-end"> 
          <template slot-scope="scope">
            <el-tag v-if="scope.row.state === 0" type="success"
              >待审核</el-tag
            >
            <el-tag v-else-if="scope.row.state === 1" type="danger"
              >通过</el-tag
            >
            <el-tag v-else type="warning">驳回</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="addUserId" label="添加人" v-if="colData[5].istrue"> </el-table-column>
        <el-table-column prop="addTime" label="添加时间" v-if="colData[6].istrue"> </el-table-column>
        <el-table-column prop="memo" label="备注" v-if="colData[7].istrue"> </el-table-column>
        <el-table-column label="操作" width="200px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
            <!-- 审核按钮 -->
             <el-tooltip
              effort="dark"
              content="审核"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-s-order"
                size="mini"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加支出记录的对话框 -->
      <el-dialog
        title="添加支出记录信息"
        :visible.sync="addDialogVisible"
        width="50%"
        @close="addDialogReset"
      >
        <!-- 内容主题区域 -->
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="100px"
        >
          <el-form-item label="科目id" prop="projectId"><el-input v-model="addForm.projectId"></el-input></el-form-item>
          <el-form-item label="金额" prop="money"><el-input v-model="addForm.money"></el-input></el-form-item>
          <el-form-item label="入账时间" prop="payTime"><el-input v-model="addForm.payTime"></el-input></el-form-item>
          <el-form-item label="会计审核状态（0待审核，1通过，2驳回）" prop="state"><el-input v-model="addForm.state"></el-input></el-form-item>
          <el-form-item label="小区id" prop="communityId"><el-input v-model="addForm.communityId"></el-input></el-form-item>
          <el-form-item label="添加人" prop="addUserId"><el-input v-model="addForm.addUserId"></el-input></el-form-item>
          <el-form-item label="添加时间" prop="addTime"><el-input v-model="addForm.addTime"></el-input></el-form-item>
          <el-form-item label="备注" prop="memo"><el-input v-model="addForm.memo"></el-input></el-form-item>
          
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改支出记录信息的对话框 -->
      <el-dialog
        title="修改支出记录信息"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editDialogClosed"
      >
        <el-form
          ref="editFormRef"
          :model="editForm"
          label-width="150px"
          :rules="editFormules"
         
        >
          <el-form-item label="科目id" prop="projectId"><el-input v-model="editForm.projectId"></el-input></el-form-item>
          <el-form-item label="金额" prop="money"><el-input v-model="editForm.money"></el-input></el-form-item>
          <el-form-item label="入账时间" prop="payTime"><el-input v-model="editForm.payTime"></el-input></el-form-item>
          <el-form-item label="会计审核状态（0待审核，1通过，2驳回）" prop="state"><el-input v-model="editForm.state"></el-input></el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="this.queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 获取支出记录列表的参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      payInfoList: [
        {
          projectId:1,
          money:99,
          payTime:1628597764,
          state:0,
          communityId: 1,
          addUserId: 259,
          addTime: 1628597764,
          memo:'暂无'        
        },
        {
          projectId:1,
          money:99,
          payTime:1628597764,
          state:1,
          communityId: 1,
          addUserId: 259,
          addTime: 1628597764,
          memo:'暂无'        
        },
        {
          projectId:1,
          money:99,
          payTime:1628597764,
          state:2,
          communityId: 1,
          addUserId: 259,
          addTime: 1628597764,
          memo:'暂无'        
        },
      ],
       colData: [
        { title: "序号", istrue: true },
        { title: "科目", istrue: true },
        { title: "金额", istrue: true },
        { title: "入账时间", istrue: true },
        { title: "会计审核状态", istrue: true },
        { title: "添加人", istrue: true },
        { title: "添加时间", istrue: true },
        { title: "备注", istrue: true },
      ],
      colOptions: [],//多选框默认全选
      colSelect: [],//所有表头标题,跟多选框组绑定的
      total: 0,
      // 控制添加对话框的显示与隐藏
      addDialogVisible: false,
      // 控制修改对话框的显示与隐藏
      editDialogVisible: false,
      // 添加房产的表单数据
      addForm: {
        projectId:'',
        money:'',
        payTime:'',
        state:'',
        communityId:'',
        addUserId:'',
        addTime:'',
        memo:''
      },
      // 查询到的编辑支出记录信息
      editForm: {},
      // 添加表单的验证规则对象
      addFormRules: {

      },
      // 修改支出记录信息表单的验证规则
      editFormules: {

      }

    }

  },
  created() {
    this.getPayInfoList()
     var _this = this;
    for (let i = 0; i < _this.colData.length; i++) {
      _this.colSelect.push(_this.colData[i].title);
      if (_this.colData[i].title == '添加人' || _this.colData[i].title == '添加时间') { //初始化不想展示的列可以放在这个条件里
        continue;
      }
      _this.colOptions.push(_this.colData[i].title);
    }
  },
    watch: {
    // 单选列实现
    colOptions(valArr) {
      var arr = this.colSelect.filter(i => valArr.indexOf(i) < 0); // 未选中
      this.colData.filter(i => {
        if (arr.indexOf(i.title) != -1) {
          i.istrue = false;
        } else {
          i.istrue = true;
        }
      });
      this.$nextTick(() => {
        this.$refs.tableDataRef.doLayout();
      });
    }
   },
  methods: {
    // 获取支出记录档案列表
    getPayInfoList() {

    },
    filterTag(value,row){
      return row.state === value
    },
    // 展示编辑支出记录的对话框
    showEditDialog() {
      this.editDialogVisible = !this.editDialogVisible
    },
    // 根据id删除支出记录
    async removeUserById() {
      // 先弹框询问用户是否确定要删除
      const confirmResult = await this.$confirm('此操作将永久删除该支出记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        return err
      })
      // 如果点击取消返回的是字符串cancel
      // 如果点击确认返回的是字符串confirm
      if (confirmResult !== 'confirm') {
        return this.$message.info('取消了删除')
      }
      return this.$message.error('删除支出记录失败')
      // 刷新列表
      this.getPayInfoList()
    },
    // 监听修改支出记录对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 监听pagesize改变的事件
    handleSizeChange(newPageSize) {
      // console.log(newPageSize)
      this.queryInfo.pagesize = newPageSize
      // PageSize改变重新拿到支出记录列表
      this.getPayInfoList()
    },
    // 监听当前页码改变的事件
    handleCurrentChange(newPageNum) {
      // console.log(newPageNum)
      this.queryInfo.pagenum = newPageNum
      this.getPayInfoList()
    },
    addDialogReset() {
      // 拿到表单的引用进行重置
      this.$refs.addFormRef.resetFields()
    },
      // 全选或取消全选实现
    filterFunHandle(type, currentItem, index) {
      if (type === 'allchecked') { // 全选
        if (this.colSelect.length === 0) {
          this.colData.forEach((result) => {
            this.colSelect.push(result.title)
          })
          this.colOptions = this.colSelect // 复选框置为复选所有值，全部选择
         
        } else {
          this.colOptions = this.colSelect // 复选框置为复选所有值，全部选择
  
        }
      } else if (type === 'cancel') { // 取消全选
        this.colOptions = [] // 复选框置为空，全部不选择
        
      }
    }
  }
}
</script>
<style lang="less" scoped>
</style>